package awesome.cpu.structure;

import awesome.cpu.utils.Mode;

public class PC extends SequentialComponent {
	
	private static boolean outputDebug = false;
	
	public final static int KEY_PC_WRITE_ENABLE = 1;
	private int mPCWriteEnable;
	
	public final static int KEY_PC_IN = 2;
	private int mPCIn;
	
	public final static int KEY_PC_OUT = 3;
	private int mPCOut;
	
	private final static PC mInstance = new PC();
	
	private PC() {}
	
	public static PC getInstance() {
		return mInstance;
	}

	@Override
	void onClockTick() {
		switch (mPCWriteEnable) {
		case 0:
			break;
		case 1:
			mPCOut = mPCIn;
			break;
		}
		if (outputDebug && Mode.getDebugLevel() >= Mode.DEBUG_LEVEL_PRINT_ALL) {
			System.out.println("PC: PCWriteEnable = "+mPCWriteEnable);
			System.out.println("PC: PCIn = "+mPCIn);
			System.out.println("PC: PCOut = "+mPCOut);
		}
	}

	@Override
	boolean setData(int keyInterface, int data) {
		if (outputDebug && Mode.getDebugLevel() >= Mode.DEBUG_LEVEL_PRINT_ALL) {
			System.out.println("PC.setData("+keyInterface+", "+data+")");
		}
		switch (keyInterface) {
		case KEY_PC_WRITE_ENABLE:
			mPCWriteEnable = data;
			return true;
		case KEY_PC_IN:
			mPCIn = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		switch (keyInterface) {
		case KEY_PC_OUT:
			return mPCOut;
		}
		return 0;
	}

	@Override
	public void stop() {
		super.stop();
		mPCOut = 0;
	}
}
